/**
 * 
 */
package com.mvity.framework.system.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.apache.shiro.crypto.SecureRandomNumberGenerator;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.springframework.stereotype.Service;

import com.mvity.framework.common.annotation.ServiceDesc;
import com.mvity.framework.common.base.BaseVO;
import com.mvity.framework.common.exception.AppException;
import com.mvity.framework.common.global.Constants;
import com.mvity.framework.common.global.ResultCode;
import com.mvity.framework.common.service.impl.BaseServiceImpl;
import com.mvity.framework.common.utils.CryptoUtil;
import com.mvity.framework.system.cache.UserAccountCache;
import com.mvity.framework.system.dao.SystemHibernateDao;
import com.mvity.framework.system.dao.SystemJdbcDao;
import com.mvity.framework.system.entity.SystemGroup;
import com.mvity.framework.system.entity.SystemGroupUser;
import com.mvity.framework.system.entity.SystemTenant;
import com.mvity.framework.system.entity.SystemTenantExtend;
import com.mvity.framework.system.entity.SystemUser;
import com.mvity.framework.system.entity.SystemUserAccount;
import com.mvity.framework.system.entity.SystemUserExtend;
import com.mvity.framework.system.service.UserService;
import com.mvity.framework.system.vo.UserAccountVO;
import com.mvity.framework.system.vo.UserVO;

/**
 * @author vity
 * @date 2012-7-23
 * @mail vity.1215@gmail.com
 */
@Service(value = "userService")
public class UserServiceImpl extends BaseServiceImpl implements UserService {

	private static final long serialVersionUID = -1583920695131707563L;

	@Resource(name = "systemHibernateDao")
	private SystemHibernateDao systemHibernateDao;

	@Resource(name = "systemJdbcDao")
	private SystemJdbcDao systemJdbcDao;

	@Override
	public void initData() throws AppException {
		SystemTenant tenant = new SystemTenant();
		tenant.setManager("平台管理员");
		tenant.setParentId("1");
		tenant.setTenantCode("1000001");
		tenant.setTenantName("平台");
		tenant.setCreateUserId("1");
		tenant.setModifyUserId("1");
		super.save(tenant);
		tenant.setTenantId(tenant.getId());
		super.update(tenant);
		SystemGroup group = new SystemGroup();
		group.setCreateUserId("1");
		group.setModifyUserId("1");
		group.setGroupName("超级管理员组");
		group.setParentId("1");
		group.setIsSystem(Constants.YES);
		group.setTenantId(tenant.getId());
		super.save(group);
		SystemUser user = new SystemUser();
		user.setCreateUserId("1");
		user.setModifyUserId("1");
		user.setNickName("super-man");
		user.setTenantId(tenant.getId());
		user.setUserName("超级管理员");
		user.setUserSex("boy");
		user.setUserType(Constants.SYSTEM_USER_TYPE_SUPER_ADMIN);
		super.save(user);
		SystemUserAccount account = new SystemUserAccount();
		account.setTenantId(tenant.getId());
		account.setCreateUserId(user.getId());
		account.setLoginName("admin");
		account.setUserPassword(new Sha256Hash("admin", new SecureRandomNumberGenerator().nextBytes(), 1024).toBase64());
		account.setUserId(user.getId());
		account.setRegisterEmail("lixn@vip.qq.com");
		account.setErrorLoginCount(0);
		account.setLastErrorLoginIp("-");
		account.setLastLoginIP("127.0.0.1");
		account.setLastLoginTime(new Date());
		account.setLoginCount(0);
		account.setRegisterIP("127.0.0.1");
		account.setLastErrorLoginTime(new Date());
		super.save(account);
		SystemTenantExtend tenantExtend = new SystemTenantExtend();
		tenantExtend.setCreateUserId(user.getId());
		tenantExtend.setTenantId(tenant.getId());
		tenantExtend.setTitle("CBF");
		tenantExtend.setLogoImage("-");
		super.save(tenantExtend);
		SystemUserExtend userExtend = new SystemUserExtend();
		userExtend.setCreateUserId(user.getId());
		userExtend.setTenantId(tenant.getId());
		userExtend.setUserId(user.getId());
		super.save(userExtend);
		SystemGroupUser userGroup = new SystemGroupUser();
		userGroup.setCreateUserId(user.getId());
		userGroup.setTenantId(tenant.getId());
		userGroup.setGroupId(group.getId());
		userGroup.setUserId(user.getId());
		super.save(userGroup);
	}

	@Override
	@ServiceDesc("验证用户登陆")
	public String userLogin(String userName, String password) throws AppException {
		UserAccountVO vo = UserAccountCache.getCache().get(userName);
		if (vo == null) {
			return ResultCode.NotFoundAccount;
		}
		if (!vo.getUserPassword().equals(CryptoUtil.encryptSHAString(password, 256))) {
			return ResultCode.AccountPasswordInvalid;
		}

		return vo.getUserId();
	}

	@Override
	@ServiceDesc("xxasdlfkj")
	public String testAspect(String xx, int x1, char x2, String[] x3, List<String> x4, Long[] x5, boolean x6, SystemUser user) throws Exception {
		try {
	        Thread.sleep(400);
        } catch (InterruptedException e) {
	        e.printStackTrace();
        }
		return "";
	}

	@Override
	public List<UserAccountVO> loadAllUserAccountToCache() throws AppException {
		List<SystemUserAccount> list = systemHibernateDao.listAll(SystemUserAccount.class);
		if (CollectionUtils.isEmpty(list)) {
			return null;
		}
		List<UserAccountVO> resultList = new ArrayList<UserAccountVO>(list.size());
		Iterator<SystemUserAccount> iterator = list.iterator();
		while (iterator.hasNext()) {
			resultList.add(BaseVO.fromEntity(UserAccountVO.class, iterator.next()));
		}
		return resultList;
	}

	@Override
	public List<UserVO> loadAllUserToCache() throws AppException {
		List<SystemUser> list = systemHibernateDao.listAll(SystemUser.class);
		if (CollectionUtils.isEmpty(list)) {
			return null;
		}
		List<UserVO> resultList = new ArrayList<UserVO>(list.size());
		Iterator<SystemUser> iterator = list.iterator();
		while (iterator.hasNext()) {
			resultList.add(BaseVO.fromEntity(UserVO.class, iterator.next()));
		}
		return resultList;
	}

}
